const logger = require("../../utils/logger")
const mysqlUtil = require("../../utils/mysqlUtil")
const redisUtil = require("../../utils/redisUtil")

module.exports = {
    // 获取部门列表
    departmentList: async ctx => {
        try {
            let sql = `
                SELECT
                    con_department.id,
                    con_department.name,
                    con_department.up_department,
                    IFNULL( t1.cnt, 0 ) cnt
                FROM
                    con_department
                    LEFT JOIN ( 
                            SELECT 
                                COUNT( 1 ) cnt, 
                                tb_user_depart_pos.department 
                            FROM 
                                tb_user_depart_pos 
                                INNER JOIN tb_user ON tb_user.uid = tb_user_depart_pos.uid
                            WHERE
                                tb_user.sta != '离职'
                            GROUP BY department 
                        ) t1 
                        ON con_department.id = t1.department
                WHERE con_department.is_del = 0
            `
            let res = await mysqlUtil.execute(sql)
            ctx.body = {code: 0, message: 'success', data: {list: res}}
        } catch (err) {
            logger.error(err)
            ctx.body = {code: -1, message: 'system error'}
        }
    },

    // 添加部门
    addDepartment: async ctx => {
        let params = ctx.request.body
        if (!params.departmentName) return ctx.body = {code: -1, message: '参数缺失'}
        try {
            let sql = 'SELECT MAX(srt) msrt FROM con_department'
            let res = await mysqlUtil.execute(sql)
            let maxSrt = res[0].msrt
            await mysqlUtil.execute('INSERT INTO con_department SET ?', [{
                name: params.departmentName,
                srt: maxSrt + 1,
                createTime: Date.now()
            }])

            // 通知刷新架构
            redisUtil.client.publish('REFRESH_CONTACT', 'aaa')

            ctx.body = {code: 0, message: 'success'}
        } catch (err) {
            logger.error(err)
            ctx.body = {code: -1, message: 'system error'}
        }
    },

    // 删除部门
    delDepartment: async ctx => {
        let params = ctx.request.body
        if (!params.id) return ctx.body = {code: -1, message: '参数缺失'}
        try {
            let sql = 'SELECT 1 FROM tb_user_depart_pos WHERE department = ? LIMIT 1'
            let res = await mysqlUtil.execute(sql, [params.id])
            if (res.length != 0) return ctx.body = {code: -1, message: '当前部门存在员工无法删除'}
            await mysqlUtil.execute('DELETE FROM con_department WHERE id = ? LIMIT ?', [params.id])

            // 通知刷新架构
            redisUtil.client.publish('REFRESH_CONTACT', 'aaa')

            ctx.body = {code: 0, message: 'success'}
        } catch (err) {
            logger.error(err)
            ctx.body = {code: -1, message: 'system error'}
        }
    },

    // 更新部门排序
    updateDepartmentSort: async ctx => {
        let params = ctx.request.body
        logger.debug('update department sort', params)
        if (!Array.isArray(params) || params.length == 0) return ctx.body = {code: -1, message: '参数错误'}
        try {
            for (let i = 0; i != params.length; i++) {
                if (!params[i].id || !params[i].srt) continue
                mysqlUtil.execute('UPDATE con_department SET srt = ? WHERE id = ? LIMIT 1', [params[i].srt, params[i].id])
            }

            // 通知刷新架构
            redisUtil.client.publish('REFRESH_CONTACT', 'aaa')

            ctx.body = {code: 0, message: 'success'}
        } catch (err) {
            logger.error(err)
            ctx.body = {code: -1, message: 'system error'}
        }
    }
}
